package com.example.controller;

import com.example.domain.Clazz;
import com.example.service.ClazzService;
import com.example.util.EasyuiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 班级信息前端控制器
 */
@RestController
@RequestMapping("/clazz")
@Slf4j
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    
    /**
     * 由于clazz数据一般都不多,就不用分页了.
     *
     * @return 所有clazz的列表,格式为Easyui datagrid格式
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public EasyuiResult<Clazz> easyuiDatagrid() {
        log.info("获取clazz列表,返回easyui datagrid数据结构");
        return clazzService.getAllClazz();
    }
    
    /**
     * 返回所有clazz数据,用户验证和下拉框
     * @return clazzlist
     */
    @RequestMapping(value = "/all", method = RequestMethod.POST)
    public List<Clazz> all() {
        log.info("获取clazz列表,返回List<Clazz>");
        return clazzService.getAll();
    }
    
    /**
     * 根据id列表删除班级
     *
     * @param ids 班级id
     * @return 结果
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(@RequestParam(value = "ids[]") int[] ids) {
        log.info("根据ids={}删除clazz", ids);
        clazzService.delete(ids);
        return "success";
    }
    
    /**
     * 对班级信息进行更新或者新增
     *
     * @param clazz 班级实体,接收数据
     * @return 处理结果返回一个新的clazz对象, 根据其id是否为空, 判断保存或者更新是否成功
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(Clazz clazz) {
        log.info("保存或者新增一个clazz对象,clazz={}", clazz);
        final Clazz result = clazzService.save(clazz);
        if (result.getId() > 0) {
            return "success";
        }
        return "error";
    }
}
